$(function () {
    $('#save-type-btn').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $('#goods-dialog');
        var name = $("input[name='name']").val();
        var small_img = $("input[name='small_img']").val();
        var big_img = $("input[name='big_img']").val();
        var priority = $("input[name='priority']").val();


        // var type_id = document.getElementsByName("type_id")[0].value;
        var submitType = self.attr("data-type");
        var type_id = self.attr('data-id');

        var url = '';
        if (submitType == 'update'){
            url = '/cms/mall/update_type/';
        }else{
            url = '/cms/mall/add_type/';
        }

        zlajax.post({
            'url':url,
            'data':{
                'name':name,
                'small_img':small_img,
                'big_img':big_img,
                'priority':priority,
                'type_id':type_id,
            },
            'success':function (data) {
                dialog.modal('hide');
                if(data['code']==200){
                    window.location.reload()
                }else{
                    const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});


$(function () {
    $(".edit-type-btn").click(function (event) {
        var self = $(this);
        var dialog = $('#goods-dialog');
        dialog.modal('show');

        var tr = self.parent().parent();
        var name = tr.attr("data-name");
        var type_img = tr.attr("data-img");
        var big_img = tr.attr("data-big");
        var priority = tr.attr("data-priority");

        var nameInput = dialog.find("input[name='name']");
        var imageInput = dialog.find("input[name='small_img']");
        var imageInput2 = dialog.find("input[name='big_img']");
        var priorityInput = dialog.find("input[name='priority']");
        var savebtn = dialog.find("#save-type-btn");

        nameInput.val(name);
        imageInput.val(type_img);
        imageInput2.val(big_img);
        priorityInput.val(priority);
        savebtn.attr("data-type",'update');
        savebtn.attr("data-id",tr.attr('data-id'));
    })
});


$(function () {
    $('.delete-type-btn').click(function (event) {
        var self = $(this)
        var tr = self.parent().parent();
        var banner_id = tr.attr('data-id');
        zlalert.alertConfirm({
            'msg':'您确定要删除这个商品类型吗？',
            'confirmCallback':function () {
                zlajax.post({
                    'url':'/cms/mall/del_type/',
                    'data':{
                        'type_id':banner_id
                    },
                    'success':function (data) {
                        if(data['code']==200){
                            window.location.reload()
                        }else{
                            const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                        }
                    }
                })
            }
        })
    })
});




